package com.tan.core.enums;

import lombok.Getter;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Tan-Bowen
 * @description 错误统一管理枚举
 * @date 2021/7/22 15:18
 */
@Getter
public enum ErrorEnum {

    SUCCESS(200, "操作成功"),
    /**网关错误*/
    PARAM_ERROR(400,"参数错误"),
    NO_AUTHORIZATION(401,"你无权访问该链接"),
    TOKEN_EXPIRED(402,"当前登录已过期，请重新登录"),
    ONLY_ONE_DEVICES(403,"您的账号已在其他设备登录"),
    ACCESS_DENIED(405,"你没有该功能的访问权限"),
    USER_INFO_FAIL(406,"获取用户信息失败"),
    /**系统模块使用错误编码500-999*/
    SERVER_ERROR(500,"服务器错误"),
    ERROR_TABLE(501,"表未定义Mapper"),
    SERVER_REMOTE_ERROR(502,"获取远程信息失败"),
    ERROR_CHARACTER(503,"非法字符"),
    ERROR_SOURCE_NULL(504,"来源为空"),
    SIGN_ERROR(505,"签名错误"),
    SQL_HANDLE_ERROR(506,"数据库操作失败"),
    SERVER_HANDLE_ERROR(507,"操作错误"),// 公用业务抛出错误
    REMIND_SETTING_ERROR(508,"该配置已存在"),
    UN_KNOW_TYPE(509,"不知道的配置类型"),


    ;

    /**
     * 错误编码
     */
    private final int code;
    /**
     * 错误信息
     */
    private final String msg;

    ErrorEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    static ConcurrentHashMap<Integer, ErrorEnum> errors = new ConcurrentHashMap<>();

    static {
        for (ErrorEnum errorEnum : values()) {
            errors.put(errorEnum.getCode(), errorEnum);
        }
    }

    public static ErrorEnum parse(int code) {
        return errors.get(code);
    }
}
